package com.hjb.service.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/** 
* @ClassName    : CalendarUtil 
* @author       : panxingwu 
* @date         : 2018年1月8日 下午9:42:11 
* @Description  : 时间工具
*/
public class CalendarUtil {
	/** 
	* @Title      :getDayOffset 
	* @param      :@param start
	* @param      :@param end
	* @param      :@return 
	* @return     :int 
	* @Description:获取两个时间之间间隔的天数
	*/
	public static int getDayOffset(Date start,Date end) {
		Calendar cal1 = Calendar.getInstance();  
        cal1.setTime(start);  
        Calendar cal2 = Calendar.getInstance();  
        cal2.setTime(end);  
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);  
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);  
        int year1 = cal1.get(Calendar.YEAR);  
        int year2 = cal2.get(Calendar.YEAR);  
        if (year1 != year2) {  //同一年  
            int timeDistance = 0;  
            for (int i = year1; i < year2; i++) {  
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {  //闰年  
                    timeDistance += 366;  
                } else {  //不是闰年  
                    timeDistance += 365;  
                }  
            }  
            return timeDistance + (day2 - day1);  
        } else { //不同年  
            return day2 - day1;  
        }  
	}
	
	/** 
	* @Title      :getMonthOffSet 
	* @param      :@param date1
	* @param      :@param date2
	* @param      :@return
	* @param      :@throws Exception 
	* @return     :int 
	* @Description:获取两个时间段之间的月份
	*/
	public static int getMonthOffSet(Date start,Date end){
        Calendar c1=Calendar.getInstance();
        Calendar c2=Calendar.getInstance();
        try {
			c1.setTime(start);
			c2.setTime(end);
		} catch (Exception e) {
			e.printStackTrace();
		}
        int year =c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR);
        //开始日期若小月结束日期
        if(year<0){
            year=-year;
            return year*12+c1.get(Calendar.MONTH)-c2.get(Calendar.MONTH);
        }
        return year*12+c2.get(Calendar.MONTH)-c1.get(Calendar.MONTH);
    }
	
	/** 
	* @Title      :getDate 
	* @param      :@param span 
	* @return     :void 
	* @Description:根据时间跨度(天)获取日期
	*/
	public static Date getDateByAddDay(int span) {
		Calendar cal = Calendar.getInstance(); 
		cal.setTime(new Date());
		cal.add(Calendar.DAY_OF_YEAR, span);
		return cal.getTime();
	}
	
	/** 
	* @param start 
	 * @Title      :getDate 
	* @param      :@param span 
	* @return     :void 
	* @Description:根据时间跨度(月)获取日期
	*/
	public static Date getDateByAddMonth(Date start, int span) {
		if(start==null) start = new Date();
		Calendar cal = Calendar.getInstance(); 
		cal.setTime(start);
		cal.add(Calendar.MONTH, span);
		return cal.getTime();
	}
	
	public static void main(String[] args) {
//		System.out.println(getMonthOffSet("20180101", "20180707"));
//		System.out.println(new Date(),new SimpleDateFormat("yyyyMMdd").format(getDateByAddMonth(3)));
	}
}

